package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    public User findByUsername(String username) {

        // 获取User
        User user = userDao.findByUsername(username);
        if(user == null){
            return null;
        }

        // 获取Role
        Integer userId = user.getId();
        Set<Role> roles = roleDao.findByUserId(userId);

        // 获取Permission
        if(roles != null && roles.size() > 0){
            for(Role role : roles){
                Integer roleId = role.getId();
                Set<Permission> permissions = permissionDao.findByRoleId(roleId);
                if(permissions != null && permissions.size() > 0){
                    role.setPermissions(permissions);
                }
            }
            user.setRoles(roles);
        }

        return user;
    }
    //分页查询
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    //添加用户，同时需要设置用户和角色的关联关系
    public void add(User user, Integer[] roleIds) {
        userDao.add(user);
        //得到最新添加角色的id
        Integer id = user.getId();
        //操作关联关系表,新增角色添加了哪些权限
        if (roleIds != null && roleIds.length != 0) {
            addRoleIds(roleIds, id);
        }
    }
    //设置角色和角色的关联关系
    private void addRoleIds(Integer[] roleIds, Integer id) {
        userDao.addUserAndRoleAssociation(roleIds, id);
    }
    //查询所有
    public List<User> findAll() {
        return userDao.findAll();
    }

    public User findById(Integer id) {
        return userDao.findById(id);
    }

    //编辑角色，同时需要更新和检查项的关联关系
    public void edit(User user, Integer[] roleIds) {
        //根据检查组id删除中间表数据（清理原有关联关系）
        userDao.deleteAssociation(user.getId());
        //向中间表(t_role_user)插入数据（建立检查组和检查项关联关系）
        addRoleIds(roleIds, user.getId());
        //更新检查组基本信息
        userDao.edit(user);
    }

    //查找关联关系
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return userDao.findRoleIdsByUserId(id);
    }

    //删除
    public void delete(Integer id) {
        //先清理关联关系
        userDao.deleteAssociation(id);
        userDao.delete(id);
    }

}